/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.siteproduct.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.siteproduct.entity.SiteProduct;
import com.thinkgem.jeesite.modules.siteproduct.service.SiteProductService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 产品生成Controller
 * @author thinkgem
 * @version 2017-08-07
 */
@Controller
@RequestMapping(value = "${adminPath}/siteproduct/siteProduct")
public class SiteProductController extends BaseController {

	@Autowired
	private SiteProductService siteProductService;
	
	@ModelAttribute
	public SiteProduct get(@RequestParam(required=false) String id) {
		SiteProduct entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = siteProductService.get(id);
		}
		if (entity == null){
			entity = new SiteProduct();
		}
		return entity;
	}
	
	@RequiresPermissions("siteproduct:siteProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(SiteProduct siteProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SiteProduct> page = siteProductService.findPage(new Page<SiteProduct>(request, response), siteProduct); 
		for (SiteProduct product : page.getList()) {
			if (StringUtils.isNotBlank(product.getCreator())) {
				User user = UserUtils.get(product.getCreator());
				if (user != null) {
					product.setCreator(user.getLoginName());
				}
			}
			if (StringUtils.isNotBlank(product.getUpdator())) {
				User user = UserUtils.get(product.getUpdator());
				if (user != null) {
					product.setUpdator(user.getLoginName());
				}
			}
		}
		model.addAttribute("page", page);
		return "modules/siteproduct/siteProductList";
	}

	@RequiresPermissions("siteproduct:siteProduct:view")
	@RequestMapping(value = "form")
	public String form(SiteProduct siteProduct, Model model) {
		if (StringUtils.isNotBlank(siteProduct.getCreator())) {
			User user = UserUtils.get(siteProduct.getCreator());
			if (user != null) {
				siteProduct.setCreator(user.getLoginName());
			}
		}
		if (StringUtils.isNotBlank(siteProduct.getUpdator())) {
			User user = UserUtils.get(siteProduct.getUpdator());
			if (user != null) {
				siteProduct.setUpdator(user.getLoginName());
			}
		}
		model.addAttribute("siteProduct", siteProduct);
		return "modules/siteproduct/siteProductForm";
	}

	@RequiresPermissions("siteproduct:siteProduct:edit")
	@RequestMapping(value = "save")
	public String save(SiteProduct siteProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, siteProduct)){
			return form(siteProduct, model);
		}
		if (siteProduct.getIsNewRecord()) {
			siteProduct.setCreator(UserUtils.getUser().getId());
			siteProduct.setCreateTime(DateUtils.getDateTime());
		}else {
			siteProduct.setUpdator(UserUtils.getUser().getId());
			siteProduct.setUpdateTime(DateUtils.getDateTime());
		}
		siteProductService.save(siteProduct);
		addMessage(redirectAttributes, "保存产品成功");
		return "redirect:"+Global.getAdminPath()+"/siteproduct/siteProduct/?orderBy=createTime desc";
	}
	
	@RequiresPermissions("siteproduct:siteProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(SiteProduct siteProduct, RedirectAttributes redirectAttributes) {
		siteProductService.delete(siteProduct);
		addMessage(redirectAttributes, "删除产品成功");
		return "redirect:"+Global.getAdminPath()+"/siteproduct/siteProduct/?orderBy=createTime desc";
	}

}